package com.lframework.xingyun.promo.impl.fullreduction;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.utils.OpLogUtil;
import java.io.Serializable;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.web.core.utils.IdUtil;
import com.lframework.starter.common.utils.ObjectUtil;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.common.utils.Assert;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.xingyun.promo.entity.MarketingFullreduction;
import com.lframework.xingyun.promo.mappers.MarketingFullreductionMapper;
import com.lframework.xingyun.promo.service.fullreduction.MarketingFullreductionService;
import com.lframework.xingyun.promo.vo.fullreduction.CreateMarketingFullreductionVo;
import com.lframework.xingyun.promo.vo.fullreduction.QueryMarketingFullreductionVo;
import com.lframework.xingyun.promo.vo.fullreduction.UpdateMarketingFullreductionVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MarketingFullreductionServiceImpl extends BaseMpServiceImpl<MarketingFullreductionMapper, MarketingFullreduction> implements MarketingFullreductionService {

    @Override
    public PageResult<MarketingFullreduction> queryPage(Integer pageIndex, Integer pageSize, QueryMarketingFullreductionVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        IPage<MarketingFullreduction> page = getBaseMapper().queryPage(vo);

        return PageResultUtil.convert(page);
    }

    @Override
    public List<MarketingFullreduction> queryList(QueryMarketingFullreductionVo vo) {

        return getBaseMapper().queryList(vo);
    }

    @Cacheable(value = MarketingFullreduction.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public MarketingFullreduction findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @OpLog(type = OtherOpLogType.class, name = "新增限时满减活动表，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String create(CreateMarketingFullreductionVo vo) {

        MarketingFullreduction data = new MarketingFullreduction();
        data.setId(IdUtil.getId());
        data.setName(vo.getName());
        data.setBeginDate(vo.getBeginDate());
        data.setEndDate(vo.getEndDate());
        data.setCycleFlag(vo.getCycleFlag());
        data.setCycleType(vo.getCycleType());
        data.setCycleTimeGroup(vo.getCycleTimeGroup());
        data.setCycleTimeRule(vo.getCycleTimeRule());
        data.setAllMemberLevel(vo.getAllMemberLevel());
        data.setMemberLevelId(vo.getMemberLevelId());
        data.setAllBuyLimit(vo.getAllBuyLimit());
        data.setCycleBuyLimit(vo.getCycleBuyLimit());
        data.setAllProduct(vo.getAllProduct());
        data.setProductId(vo.getProductId());
        data.setUseCoupon(vo.getUseCoupon());
        data.setUseFulldiscount(vo.getUseFulldiscount());
        data.setUseFullprice(vo.getUseFullprice());
        data.setUseFullgiftactivity(vo.getUseFullgiftactivity());
        data.setAllShop(vo.getAllShop());
        data.setShopId(vo.getShopId());
        data.setReductionType(vo.getReductionType());
        data.setPromType(vo.getPromType());
        data.setPromItem(vo.getPromItem());

        getBaseMapper().insert(data);

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);

        return data.getId();
    }

    @OpLog(type = OtherOpLogType.class, name = "修改限时满减活动表，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(UpdateMarketingFullreductionVo vo) {

        MarketingFullreduction data = getBaseMapper().selectById(vo.getId());
        if (ObjectUtil.isNull(data)) {
            throw new DefaultClientException("限时满减活动表不存在！");
        }

        LambdaUpdateWrapper<MarketingFullreduction> updateWrapper = Wrappers.lambdaUpdate(MarketingFullreduction.class)
                .set(MarketingFullreduction::getName, vo.getName())
                .set(MarketingFullreduction::getBeginDate, vo.getBeginDate())
                .set(MarketingFullreduction::getEndDate, vo.getEndDate())
                .set(MarketingFullreduction::getCycleFlag, vo.getCycleFlag())
                .set(MarketingFullreduction::getCycleType, vo.getCycleType())
                .set(MarketingFullreduction::getCycleTimeGroup, vo.getCycleTimeGroup())
                .set(MarketingFullreduction::getCycleTimeRule, vo.getCycleTimeRule())
                .set(MarketingFullreduction::getAllMemberLevel, vo.getAllMemberLevel())
                .set(MarketingFullreduction::getMemberLevelId, vo.getMemberLevelId())
                .set(MarketingFullreduction::getAllBuyLimit, vo.getAllBuyLimit())
                .set(MarketingFullreduction::getCycleBuyLimit, vo.getCycleBuyLimit())
                .set(MarketingFullreduction::getAllProduct, vo.getAllProduct())
                .set(MarketingFullreduction::getProductId, vo.getProductId())
                .set(MarketingFullreduction::getUseCoupon, vo.getUseCoupon())
                .set(MarketingFullreduction::getUseFulldiscount, vo.getUseFulldiscount())
                .set(MarketingFullreduction::getUseFullprice, vo.getUseFullprice())
                .set(MarketingFullreduction::getUseFullgiftactivity, vo.getUseFullgiftactivity())
                .set(MarketingFullreduction::getAllShop, vo.getAllShop())
                .set(MarketingFullreduction::getShopId, vo.getShopId())
                .set(MarketingFullreduction::getReductionType, vo.getReductionType())
                .set(MarketingFullreduction::getPromType, vo.getPromType())
                .set(MarketingFullreduction::getPromItem, vo.getPromItem())
                .eq(MarketingFullreduction::getId, vo.getId());

        getBaseMapper().update(updateWrapper);

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);
    }

    @CacheEvict(value = MarketingFullreduction.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

